package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.mapper.ChinaMapper;
import com.woniuxy.pojo.China;
import com.woniuxy.service.ChinaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* @author 29331
* @description 针对表【t_china】的数据库操作Service实现
* @createDate 2025-05-22 10:46:13
*/
@Service
public class ChinaServiceImpl extends ServiceImpl<ChinaMapper, China>
    implements ChinaService {
    @Autowired
    ChinaMapper chinaMapper;

    @Autowired
    RedisTemplate redisTemplate;

 /*   @Override
    public List<China> getAllProvinces() {
        LambdaQueryWrapper<China> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(China::getPid,0);
        List<China> provinces = chinaMapper.selectList(wrapper);
        return provinces;
    }

    @Override
    public List<China> getAllCities(Integer provinceId) {
        LambdaQueryWrapper<China> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(China::getPid,provinceId);
        List<China> cities = chinaMapper.selectList(wrapper);
        return cities;
    }

    @Override
    public List<China> getAllAreas(Integer cityId) {
        LambdaQueryWrapper<China> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(China::getPid,cityId);
        List<China> areas = chinaMapper.selectList(wrapper);
        return areas;
    }*/


    @Override
    public List<China> getAddress(Integer id, String key) {
        Boolean flg = redisTemplate.hasKey(key);
        if (flg) {
            Set<China> range = redisTemplate.opsForZSet().range(key, 0, -1);
            List<China> address = new ArrayList<>();
            range.forEach(item -> address.add(item));
            return address;
        } else {
            LambdaQueryWrapper<China> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(China::getPid, id);
            List<China> address = chinaMapper.selectList(wrapper);
            for (China china : address) {
                redisTemplate.opsForZSet().add(key, china, china.getId());
            }
            return address;
        }
    }
}




